INF-510, v0.22, Claudio Torres, ctorres@inf.utfsm.cl. DI-UTFSM

Textbook: Lloyd N. Trefethen, Spectral Methods in MATLAB, SIAM, Philadelphia, 2000

Differentiation by Interpolation


In [7]:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import scipy.sparse.linalg as sp
import sympy as sym
from scipy.linalg import toeplitz
import ipywidgets as widgets
import matplotlib as mpl
mpl.rcParams['font.size'] = 14
mpl.rcParams['axes.labelsize'] = 20
mpl.rcParams['xtick.labelsize'] = 14
mpl.rcParams['ytick.labelsize'] = 14
sym.init_printing()

Defining symbolic variables


In [9]:
xj, h, x = sym.symbols('xj h x', reals=True)
u1, u2, u3, u4, u5 = sym.symbols('u1 u2 u3 u4 u5', reals=True)

Do you remember Lagrange interpolation? Please do


In [10]:
# The little Lagrange polynomials
def l_Lagrange(X,x,i):
    l=1
    n=len(X)
    for k in np.arange(i):
        l*=(x-X[k])
    for k in np.arange(i+1,n):
        l*=(x-X[k])
    return l
# The Lagrange polynomials
def L_Lagrange(X,x,i):
    num = l_Lagrange(X,x,i)
    den = l_Lagrange(X,X[i],i)
    L=num/den
    return L
# The Lagrange interpolation
# X : x - data points
# x : the symbolic variable
# Y : y - data points
def P_Lagrange(X,x,Y):
    P=0
    n=len(X)
    for i in np.arange(n):
        P+=Y[i]*L_Lagrange(X,x,i)
    return P

Just a small example of a interpolation of 2 points


In [6]:
# x - data points
X=(0,1,2,3,4,5,6)
# y - data points
Y=(0,1,-2,1,0,2,7)
# The interpolation
P=P_Lagrange(X,x,Y)
# Just showing the polynomial. Do you see the Lagrange polynomials?
P


Out[6]:
$$- \frac{x}{120} \left(x - 6\right) \left(x - 5\right) \left(x - 4\right) \left(x - 3\right) \left(x - 2\right) - \frac{x}{24} \left(x - 6\right) \left(x - 5\right) \left(x - 4\right) \left(x - 3\right) \left(x - 1\right) - \frac{x}{36} \left(x - 6\right) \left(x - 5\right) \left(x - 4\right) \left(x - 2\right) \left(x - 1\right) - \frac{x}{60} \left(x - 6\right) \left(x - 4\right) \left(x - 3\right) \left(x - 2\right) \left(x - 1\right) + \frac{7 x}{720} \left(x - 5\right) \left(x - 4\right) \left(x - 3\right) \left(x - 2\right) \left(x - 1\right)$$

In [11]:
# And a simplified version of the polinomial!
sym.simplify(P)


Out[11]:
$$\frac{x}{720} \left(- 61 x^{5} + 1137 x^{4} - 8005 x^{3} + 26295 x^{2} - 39454 x + 20808\right)$$

Plotting what we got


In [12]:
# We need to plot in a finer grid
xx=np.linspace(min(X),max(X),1000)
# Here we create a lambda function for our interpolation
Pl=sym.lambdify(x,P)
# The next line vectorize the lambda function, it is not need here but it can be used
# Plv=np.vectorize(Pl) 
plt.figure()
# Plotting the interpolated data
plt.plot(xx,Pl(xx),label='$P(x)$')
# Plotting the data points
plt.plot(np.array(X),np.array(Y),'k.',markersize=20,label='Data points')
plt.grid(True)
plt.ylim([np.min(Pl(xx))-1,np.max(Pl(xx))+1])
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.legend(loc='best')
plt.show()


Let's find the 4th order differentiation approximation


In [21]:
# x - data points. Notice that they are all symbolic variables!
X=(xj-2*h,xj-h,xj,xj+h,xj+2*h)
# y - data points. Also symbolic variables!
Y=(u1,u2,u3,u4,u5)
## In-class
#X=(xj-h,xj,xj+h)
#Y=(u1,u2,u3)
# The Lagrange interpolation still works!
P=P_Lagrange(X,x,Y)
P


Out[21]:
$$\frac{u_{1}}{24 h^{4}} \left(x - xj\right) \left(- 2 h + x - xj\right) \left(- h + x - xj\right) \left(h + x - xj\right) - \frac{u_{2}}{6 h^{4}} \left(x - xj\right) \left(- 2 h + x - xj\right) \left(- h + x - xj\right) \left(2 h + x - xj\right) + \frac{u_{3}}{4 h^{4}} \left(- 2 h + x - xj\right) \left(- h + x - xj\right) \left(h + x - xj\right) \left(2 h + x - xj\right) - \frac{u_{4}}{6 h^{4}} \left(x - xj\right) \left(- 2 h + x - xj\right) \left(h + x - xj\right) \left(2 h + x - xj\right) + \frac{u_{5}}{24 h^{4}} \left(x - xj\right) \left(- h + x - xj\right) \left(h + x - xj\right) \left(2 h + x - xj\right)$$

In [22]:
# Here we compute the derivative of the polynomian P(x), i.e. we obtain P'(x) and we definy it as Pp (P-prime).
Pp=sym.diff(P,x)
Pp


Out[22]:
$$\frac{u_{1}}{24 h^{4}} \left(x - xj\right) \left(- 2 h + x - xj\right) \left(- h + x - xj\right) + \frac{u_{1}}{24 h^{4}} \left(x - xj\right) \left(- 2 h + x - xj\right) \left(h + x - xj\right) + \frac{u_{1}}{24 h^{4}} \left(x - xj\right) \left(- h + x - xj\right) \left(h + x - xj\right) + \frac{u_{1}}{24 h^{4}} \left(- 2 h + x - xj\right) \left(- h + x - xj\right) \left(h + x - xj\right) - \frac{u_{2}}{6 h^{4}} \left(x - xj\right) \left(- 2 h + x - xj\right) \left(- h + x - xj\right) - \frac{u_{2}}{6 h^{4}} \left(x - xj\right) \left(- 2 h + x - xj\right) \left(2 h + x - xj\right) - \frac{u_{2}}{6 h^{4}} \left(x - xj\right) \left(- h + x - xj\right) \left(2 h + x - xj\right) - \frac{u_{2}}{6 h^{4}} \left(- 2 h + x - xj\right) \left(- h + x - xj\right) \left(2 h + x - xj\right) + \frac{u_{3}}{4 h^{4}} \left(- 2 h + x - xj\right) \left(- h + x - xj\right) \left(h + x - xj\right) + \frac{u_{3}}{4 h^{4}} \left(- 2 h + x - xj\right) \left(- h + x - xj\right) \left(2 h + x - xj\right) + \frac{u_{3}}{4 h^{4}} \left(- 2 h + x - xj\right) \left(h + x - xj\right) \left(2 h + x - xj\right) + \frac{u_{3}}{4 h^{4}} \left(- h + x - xj\right) \left(h + x - xj\right) \left(2 h + x - xj\right) - \frac{u_{4}}{6 h^{4}} \left(x - xj\right) \left(- 2 h + x - xj\right) \left(h + x - xj\right) - \frac{u_{4}}{6 h^{4}} \left(x - xj\right) \left(- 2 h + x - xj\right) \left(2 h + x - xj\right) - \frac{u_{4}}{6 h^{4}} \left(x - xj\right) \left(h + x - xj\right) \left(2 h + x - xj\right) - \frac{u_{4}}{6 h^{4}} \left(- 2 h + x - xj\right) \left(h + x - xj\right) \left(2 h + x - xj\right) + \frac{u_{5}}{24 h^{4}} \left(x - xj\right) \left(- h + x - xj\right) \left(h + x - xj\right) + \frac{u_{5}}{24 h^{4}} \left(x - xj\right) \left(- h + x - xj\right) \left(2 h + x - xj\right) + \frac{u_{5}}{24 h^{4}} \left(x - xj\right) \left(h + x - xj\right) \left(2 h + x - xj\right) + \frac{u_{5}}{24 h^{4}} \left(- h + x - xj\right) \left(h + x - xj\right) \left(2 h + x - xj\right)$$

In [23]:
# Now we evaluate P'(x) at x=x_j.
PpE=Pp.subs({x:xj})
PpE


Out[23]:
$$\frac{u_{1}}{12 h} - \frac{2 u_{2}}{3 h} + \frac{2 u_{4}}{3 h} - \frac{u_{5}}{12 h}$$

Class work

How to you find a 6th order approximation? Please do it


In [ ]:

How do you find a second order approximation on a left boundary? i.e. say you have $u_1(x_1)$, $u_2(x_2)$ and $u_3(x_3)$, interpolate them and compute the derivative at $x_1$.


In [ ]: